<?php

class RolesController extends AppController {
    
    // define controller name
    var $name = 'Roles';
    
    // define list of models to use 
    var $uses = array('Role','Right');
    
    // set controller domain
    var $controllerDomain = 'users';
    
    public function beforeFilter() {
        parent::beforeFilter();
        
        //$this->log($this->Security,'mylog');
        
        //$this->Security->validatePost = 0;
        //$this->Security->unlockedFields = array('rights');
        //$this->Security->csrfUseOnce  = false;
        //$this->Security->csrfExpires  = '+1 hour';
    }
    
    public function isAuthorized() {
        
        $valid = parent::isAuthorized();
        if ($valid == false) {
            return false;
        }
        
        return true;
    }
     
    // create new record
    public function create() {
        
        // get parent role
        $parentRole = $this->Role->getActiveRecord($this->Auth->user('role_id'));
        if ($parentRole == null) {
            $this->Session->setFlash(__('Invalid request',true));
            $this->redirect(array('controller' => 'users','action' => 'logout')); 
        }

        // get available rights
        $availableRights = $this->Role->getActiveRights($this,$parentRole['Role']['id']);
        $this->set('availableRights',$availableRights);
      
        // create role
        if ($this->request->is('post')) {
            
            $this->request->data['Role']['parent_id'] = $parentRole['Role']['id'];
            $this->request->data['Role']['hierarchy'] = $parentRole['Role']['hierarchy'] . $parentRole['Role']['id'] . "|";
            
            if ($this->Role->sanitizeAll() && $this->Role->validates() && $this->Role->save($this->request->data)) {     
                
                $this->Role->modifyRights($this,$this->Role->id,$this->request->data['Role']['rights'],$availableRights);
                
                $this->Session->setFlash(__('Role created',true)); 
                $this->redirect(array('controller' => 'roles','action' => 'index'));            
            }      
            
            $this->Session->setFlash(__('Please correct the mistakes below',true)); 
        }
    }
    
    public function delete() {
       
        $this->setAjaxLayout();
        
        $response = array();
        
        $ids = $this->request->params['ids'];
        
        return json_encode($response);
        
        
        if (!$this->Role->activeRecordExists($id)) {   
            $this->Session->setFlash(__('Invalid request',true));
            $this->redirect(array('controller' => 'roles','action' => 'index'));           
        }
        
        if ($this->Role->delete($id)) {
            $this->removeFromHierarchy(null,$id);
            $this->cleanRights($this,$id);
            $this->Session->setFlash(__('Role deleted',true));
        } else {
            $this->Session->setFlash(__('Operation failed',true));
        }
        
    }
    
    public function edit($id = null) {
        
        // get db record
        $dbRole = $this->Role->getActiveRecord($id);$this->Role->find('first',array('conditions' => array('id' => $id)));
        if ($dbRole == null) {
            $this->Session->setFlash(__('Invalid request',true));
            $this->redirect(array('controller' => 'roles','action' => 'index'));
        }
        
        // get parent role
        $parentRole = $this->Role->getParentRole($id);
        if ($parentRole == null) {
            $this->Session->setFlash(__('Invalid request',true));
            $this->redirect(array('controller' => 'users','action' => 'logout')); 
        }
        
        // get available rights
        $availableRights = $this->Role->getActiveRights($this,$parentRole['Role']['id']);
        $this->set('availableRights',$availableRights);
        
        // set data for modifications
        if ($this->request->is('get')) {
            $this->request->data = $dbRole;
            $this->request->data['rights'] = $this->Role->getActiveRights($this,$id);
        }
        
        // process modifications
        if ($this->request->is('post')) {
            
            $this->Role->create();
            $this->Role->id = $id;
            
            if ($this->Role->sanitizeAll() && $this->Role->validates() && $this->Role->save($this->request->data)) { 
                
                $this->Role->modifyRights($this,$id,$this->request->data['Role']['rights'],$availableRights);
                
                $this->Session->setFlash(__('Role updated',true));
                $this->redirect(array('controller' => 'roles','action' => 'index'));              
            }
            
            $this->Session->setFlash(__('Please correct the mistakes below',true));
        } 
    }
    
    public function view($id = null) {
        //todo
    }
    
    public function index() {
        //todo
    }
    
    public function getItems() {
        //todo
    }
    
}

?>